package c.x.jy.service.utils.mqtt;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import c.x.jy.common.mqtt.ReadConfig;
 

public class PushCallback implements MqttCallback,MqttCallbackExtended{
 
	private MqttClient mqttClient;
	private OutPaperBackMsg outPaperBackMsg;

	public	PushCallback( OutPaperBackMsg outPaperBackMsg){

     this.outPaperBackMsg =outPaperBackMsg;
	}
	public void init(MqttClient client) {
		this.mqttClient=client;
	}

	
	@Override
	public void connectionLost(Throwable arg0) {
		   // 连接丢失后，一般在这里面进行重连
        System.out.println("连接断开，可以做重连");
      //  MQClientUtil.delete(ReadConfig.getMqttServerClient());
  //    MqttSeverClient.init();
 
	}

	@Override
	public void deliveryComplete(IMqttDeliveryToken token) {
		// TODO Auto-generated method stub
		  System.out.println("deliveryComplete---------" + token.isComplete());
		  

	}

	@Override
	public void messageArrived(String topic, MqttMessage message) throws Exception {
		// TODO Auto-generated method stub
      
        
        
        // subscribe后得到的消息会执行到这里面
        System.out.println("接收消息主题:" + topic);
        System.out.println("接收消息Qos:" + message.getQos());
        System.out.println("接收消息内容:" + new String(message.getPayload()));
        
        String back=new String(message.getPayload());
        System.out.println("接收消息内容:" +back);
   try{
	   outPaperBackMsg.getbackdata(back);
   }catch (Exception e){

   }

		 
      
        
	}
	

	@Override
	public void connectComplete(boolean reconnect, String serverURI) {
		  System.out.println("complete:"+serverURI);
		  mqttClient.setCallback(this);
		  try {
			  System.out.println("订阅!");
			mqttClient.subscribe(ReadConfig.getMqttServerClient(), 1);
		} catch (MqttException e) {
			e.printStackTrace();
		}
		
	}


}
